package org.itzheng.myrxframe.bugly;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.UpgradeInfo;
import com.tencent.bugly.beta.download.DownloadListener;
import com.tencent.bugly.beta.download.DownloadTask;

import org.itzheng.myrxframe.R;

/**
 * 更新弹窗demo
 * android:theme="@android:style/Theme.Translucent"
 * 自定义主题
 * <style name="BuglyUpgrade" parent="Theme.AppCompat.Light.NoActionBar">
 * <item name="android:windowBackground">@android:color/transparent</item>
 * <item name="android:windowIsTranslucent">true</item>
 * <item name="android:windowAnimationStyle">@android:style/Animation.InputMethod</item>
 * <item name="android:windowFullscreen">true</item>
 * </style>
 * android:theme="@style/BuglyUpgrade"
 */
public class UpgradeActivity extends Activity {
    private TextView tv;
    private TextView title;
    private TextView version;
    private TextView size;
    private TextView time;
    private TextView content;
    private Button cancel;
    private Button start;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setWindowAnimations(android.R.style.Animation_InputMethod);
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        {//设置状态栏和底部导航栏透明
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.activity_upgrade);
        tv = getView(R.id.tv);
        title = getView(R.id.title);
        version = getView(R.id.version);
        size = getView(R.id.size);
        time = getView(R.id.time);
        content = getView(R.id.content);
        cancel = getView(R.id.cancel);
        start = getView(R.id.start);
            /*获取下载任务，初始化界面信息*/
        updateBtn(Beta.getStrategyTask());
        tv.setText(tv.getText().toString() + Beta.getStrategyTask().getSavedLength() + "");
            /*获取策略信息，初始化界面信息*/
        UpgradeInfo upgradeInfo = Beta.getUpgradeInfo();
//        {
//            StringBuilder info = new StringBuilder();
//            info.append("id: ").append(upgradeInfo.id).append("\n");
//            info.append("标题: ").append(upgradeInfo.title).append("\n");
//            info.append("升级说明: ").append(upgradeInfo.newFeature).append("\n");
//            info.append("versionCode: ").append(upgradeInfo.versionCode).append("\n");
//            info.append("versionName: ").append(upgradeInfo.versionName).append("\n");
//            info.append("发布时间: ").append(upgradeInfo.publishTime).append("\n");
//            info.append("安装包Md5: ").append(upgradeInfo.apkMd5).append("\n");
//            info.append("安装包下载地址: ").append(upgradeInfo.apkUrl).append("\n");
//            info.append("安装包大小: ").append(upgradeInfo.fileSize).append("\n");
//            info.append("弹窗间隔（ms）: ").append(upgradeInfo.popInterval).append("\n");
//            info.append("弹窗次数: ").append(upgradeInfo.popTimes).append("\n");
//            info.append("发布类型（0:测试 1:正式）: ").append(upgradeInfo.publishType).append("\n");
//            info.append("弹窗类型（1:建议 2:强制 3:手工）: ").append(upgradeInfo.upgradeType).append("\n");
//            info.append("图片地址：").append(upgradeInfo.imageUrl);
//        }
        title.setText(title.getText().toString() + upgradeInfo.title);
        version.setText(version.getText().toString() + upgradeInfo.versionName);
        size.setText(size.getText().toString() + upgradeInfo.fileSize + "");
        time.setText(time.getText().toString() + upgradeInfo.publishTime + "");
        content.setText(upgradeInfo.newFeature);

            /*为下载按钮设置监听*/
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DownloadTask task = Beta.startDownload();
                updateBtn(task);
//                if (task.getStatus() == DownloadTask.DOWNLOADING) {
//                    finish();
//                }
            }
        });

            /*为取消按钮设置监听*/
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Beta.cancelDownload();
                finish();
            }
        });

            /*注册下载监听，监听下载事件*/
        Beta.registerDownloadListener(new DownloadListener() {
            @Override
            public void onReceive(DownloadTask task) {
                updateBtn(task);
                tv.setText(task.getSavedLength() + "");
            }

            @Override
            public void onCompleted(DownloadTask task) {
                updateBtn(task);
                tv.setText(task.getSavedLength() + "");
            }

            @Override
            public void onFailed(DownloadTask task, int code, String extMsg) {
                updateBtn(task);
                tv.setText("failed");

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
            /*注销下载监听*/
        Beta.unregisterDownloadListener();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }

    public void updateBtn(DownloadTask task) {
            /*根据下载任务状态设置按钮*/
        switch (task.getStatus()) {
            case DownloadTask.INIT:
            case DownloadTask.DELETED:
            case DownloadTask.FAILED: {
                start.setText("开始下载");
            }
            break;
            case DownloadTask.COMPLETE: {
                start.setText("安装");
            }
            break;
            case DownloadTask.DOWNLOADING: {
                start.setText("暂停");
            }
            break;
            case DownloadTask.PAUSED: {
                start.setText("继续下载");
            }
            break;
        }
    }

    public <T extends View> T getView(int id) {
        return (T) findViewById(id);
    }
}